 #include <reg51.h>  		
#include "lcd.h"
#include<stdio.h>

sbit DQ  =  P1^0;	// connect with DS1820 Data pin
unsigned char MyTemp[9];
void DelayUs(int us);
void display(); 
 void display1(unsigned char *name,unsigned int delay);
 void wait_state(unsigned int q);
 void delay();
 void delay1();
 void lcd_en ();
 char buf[20];


//----------------------------------------
// Reset DS1820
//----------------------------------------
bit ResetDS1820(void)
{
	bit presence;
	DQ = 0; 		//pull DQ line low
	DelayUs(29); 	// leave it low for about 490us
	DQ = 1; 		// allow line to return high
	DelayUs(3); 	// wait for presence 55 uS
	presence = DQ; 	// get presence signal
	DelayUs(25); 	// wait for end of timeslot 316 uS 
	return(presence); // presence signal returned
} 	// 0=presence, 1 = no part
//-----------------------------------------
// Read one bit from DS1820
//-----------------------------------------
bit ReadBit(void)
{
	unsigned char i=0;
	DQ = 0; 	// pull DQ low to start timeslot
	DQ=1;
	for (i=0; i<3; i++); // delay 17 us from start of timeslot
	return(DQ); // return value of DQ line
}
//-----------------------------------------
// Write one bit to DS1820
//-----------------------------------------
void WriteBit(bit Dbit)
{

   DQ=0;	
	DQ = Dbit ? 1:0;
	DelayUs(5); 			// delay about 39 uS
	DQ = 1;
}
//-----------------------------------------
// Read 1 byte from DS1820
//-----------------------------------------
unsigned char ReadByte(void)
{
	unsigned char i;
	unsigned char Din = 0;
	for (i=0;i<8;i++)
	{
		Din|=ReadBit()? 0x01<<i:Din;
		DelayUs(6); 
	}
	return(Din);
}
void WriteByte(unsigned char Dout)
{
	unsigned char i;
	for (i=0; i<8; i++) // writes byte, one bit at a time
	{	    
		WriteBit((bit)(Dout & 0x1)); 		// write bit in temp into
		Dout = Dout >> 1;
	}
	DelayUs(5);
}
//-----------------------------------------
// Read temperature
//-----------------------------------------
void ReadTemp(unsigned char * buff)
{
	unsigned char n;
	
	EA=0;	// disable all interrupt
	ResetDS1820();
    WriteByte(0xcc);  // skip ROM
    WriteByte(0x44);  // perform temperature conversion
    while (ReadByte()==0xff); // wait for conversion complete	
    ResetDS1820();
    WriteByte(0xcc);  // skip ROM
    WriteByte(0xbe);  // read the result
    
    for (n=0; n<9; n++)     // read 9 bytes but, use only one byte
    {
       buff[n]=ReadByte();  // read DS1820
    }
	EA=1;
}
void DelayMs(unsigned int count) 
{  // mSec Delay 11.0592 Mhz 
    unsigned int i;		       		// RIDE v6.1.6 
    while(count) {
        i = 115; 
		while(i>0) i--;
        count--;
    }
}
//////////////////////////////////////////////

                 
 void disp(int name1)   //LCD display routine for digits display 
 	 {
                 	
 		    rs = 0x01;
 		 	 P0 = name1;
 			     delay1();
   }  

  void init_lcd()
              { 
                unsigned char x[] = {0x38,0x0c,0x01,0x06,0x80}; 
                unsigned char i;
 	             for(i=0;i<=4;i++)
 	             {
 	              rs=0;
            	  P0       =x[i];
            	  lcd_en ();
            	  delay1();
            	   }
               } 

void lcd_first_line()

{
 LCD_command(0x80);
}
void lcd_second_line()
{
 LCD_command(0xc0);
}


//----------------------------------------
// DELAY at 11.0592MHz crystal.
// Calling the routine takes about 22us, and then
// each count takes another 17us.
// //----------------------------------------
void DelayUs(int us)
{
	int i;
	for (i=0; i<us; i++);
}
 void delay1()  //Function call for delay routine
  {
	  unsigned int i =10;
	  while(i--);
  }
   void delay2()  //Function call for delay routine
  {
	  unsigned int i =10000;
	  while(i--);
  }
  
void InitSerial(void)
{
  	SCON = 0x52;    // setup serial port control 
  	TMOD = 0x20;    // hardware (9600 BAUD @11.05592MHZ) 
  	TH1  = 0xFD;    // TH1
	TR1	 = 1;  		// Timer 1 on
}

void main()
 {
unsigned char tp,tpd;
init_lcd();
  while(1)
  {
      ReadTemp(&MyTemp[0]);        
		tp  = MyTemp[0] >> 1;
		tpd = ((MyTemp[0] >> 1)&1) ? 5:0;
//	printf("temp=%d %d\n\t\r",tp,tpd);	
      LCD_puts("temp:"); //display in LCD       
      delay1();   //call for delay routine
      
      LCD_command(0x80);
      disp(((tp%1000)/100)+48);// LCD display for corresponding digit mask 
      disp(((tp%100)/10)+48);// ,,
      LCD_puts(".");                    // and convert to ascii 0 for 48
      disp(((tpd%10)/1)+48); // ,
  ///    delay2();
 //      LCD_command(0x01);
		
 
   
   }

 } 

